//
//  CarViewController.m
//  B
//
//  Created by 王展 on 15/11/7.
//  Copyright © 2015年 wangzhan. All rights reserved.
//

#import "CarViewController.h"
#import "BaseView.h"
#import "AFNetworking.h"

#import "Index.h"
#import "Weather.h"
#import "Prev.h"
#import "Next.h"

@interface CarViewController ()

@property(nonatomic,strong)UIImageView *backImageView;
@property(nonatomic,strong)UIImage *backImage;

@property(nonatomic,strong)UILabel *cityName;
@property(nonatomic,strong)UILabel *climatic;
@property(nonatomic,strong)UILabel *temp;
@property(nonatomic,strong)UILabel *text0;
@property(nonatomic,strong)UILabel *info;
@property(nonatomic,strong)UILabel *publish;
@property(nonatomic,strong)UILabel *title1;
@property(nonatomic,strong)UILabel *text1;
@property(nonatomic,strong)UILabel *title2;
@property(nonatomic,strong)UILabel *text2;
@property(nonatomic,strong)UIImageView *icon;

@end

@implementation CarViewController

- (void) viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    for (UIView *view in self.tabBarController.view.subviews) {
        if ([view isKindOfClass:[BaseView class]]) {
            view.hidden = YES;
        }
    }
}

- (void) viewWillDisappear:(BOOL)animated {
    
    [super viewWillDisappear:animated];
    for (UIView *view in self.tabBarController.view.subviews) {
        if ([view isKindOfClass:[BaseView class]]) {
            view.hidden = NO;
        }
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    //初始化
    self.cityName = [[UILabel alloc] init];
    self.climatic = [[UILabel alloc] init];
    self.temp = [[UILabel alloc] init];
    self.icon = [[UIImageView alloc] init];
    self.text0 = [[UILabel alloc] init];
    self.info = [[UILabel alloc] init];
    self.publish = [[UILabel alloc] init];
    self.title1 = [[UILabel alloc] init];
    self.text1 = [[UILabel alloc] init];
    self.title2 = [[UILabel alloc] init];
    self.text2 = [[UILabel alloc] init];
    
    UIImage *leftImage = [UIImage imageNamed:@"导航栏_button_back_用于白和灰.png"];
    leftImage = [leftImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:leftImage style:UIBarButtonItemStylePlain target:self action:@selector(leftButtonAction)];
    
    self.navigationItem.title = @"洗车指数";
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self createWeatherOrdinaryBackView];
    [self getcarData];
    [self createView];
}

-(void)getcarData
{
    [MBProgressHUD showHUDAddedTo:self.backImageView animated:YES].labelText = @"加载中...";
    
    NSString *url_string = [NSString stringWithFormat:@"http://tq.91.com/api/?id=xc&chl=otSy9Zonl%%2Fk5CTkt33reOQ%%3D%%3D&isfamily=0&cityName=&spid=1&osv=9.0&imei=3A0F8933-0FBC-4B7B-A054-18E0C3196545&city=%@&cAct=2&sv=3.12.0.1&act=211&nt=10&pid=115&dm=iPhone7%%2C1&mt=1",self.today.cityid];
    
    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    
    [manager GET:url_string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSLog(@"使用AFN进行get请求 ===  %@",responseObject);
        NSDictionary *bigDic = responseObject;
        Index *index = [[Index alloc] init];
        [index setValuesForKeysWithDictionary:bigDic];
        Weather *weather = [[Weather alloc] init];
        [weather setValuesForKeysWithDictionary:index.weather];
        Prev *prev = [[Prev alloc] init];
        [prev setValuesForKeysWithDictionary:index.prev];
        Next *next = [[Next alloc] init];
        [next setValuesForKeysWithDictionary:index.next];
        
        self.cityName.text = weather.cityName;
        self.climatic.text = weather.climatic;
        self.temp.text = weather.temp;
        self.text0.text = index.text;
        self.info.text = index.info;
        self.publish.text = index.publish;
        self.title1.text = [NSString stringWithFormat:@"%@:",prev.title];
        self.text1.text = prev.text;
        self.title2.text = [NSString stringWithFormat:@"%@:",next.title];
        self.text2.text = next.text;
        
        self.icon.image = [UIImage imageNamed:@"carlogo"];
        
        [MBProgressHUD hideAllHUDsForView:self.backImageView animated:YES];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"失败==== %@",error);
        
    }];
}

-(void)leftButtonAction
{
    [self.navigationController popToRootViewControllerAnimated:NO];
}

-(void)createWeatherOrdinaryBackView
{
    self.backImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    self.backImage = [UIImage imageNamed:@"back"];
    self.backImageView.image = self.backImage;
    [self.view addSubview:self.backImageView];
    
}

-(void)createView
{
    self.cityName.frame = CGRectMake(130 * XWIDTH, 30 * XHIGHT, 120 * XWIDTH, 60 * XHIGHT);
    //    self.cityName.backgroundColor = [UIColor redColor];
    [self.backImageView addSubview:self.cityName];
    self.cityName.textColor = [UIColor whiteColor];
    self.cityName.font = [UIFont systemFontOfSize:25 * XWIDTH];
    [self.cityName setTextAlignment:NSTextAlignmentCenter];
    
    self.temp.frame = CGRectMake(100 * XWIDTH, 100 * XHIGHT, 150 * XWIDTH, 25 * XHIGHT);
    //    self.temp.backgroundColor = [UIColor redColor];
    [self.backImageView addSubview:self.temp];
    self.temp.textColor = [UIColor whiteColor];
    self.temp.font = [UIFont systemFontOfSize:17 * XWIDTH];
    
    self.climatic.frame = CGRectMake(240 * XWIDTH, 100 * XHIGHT, 55 * XWIDTH, 25 * XHIGHT);
    //    self.climatic.backgroundColor = [UIColor redColor];
    [self.backImageView addSubview:self.climatic];
    self.climatic.textColor = [UIColor whiteColor];
    self.climatic.font = [UIFont systemFontOfSize:17 * XWIDTH];
    
    self.icon.frame = CGRectMake(80 * XWIDTH, 140 * XHIGHT, 215 * XWIDTH, 215 * XWIDTH);
    //    self.icon.backgroundColor = [UIColor redColor];
    [self.backImageView addSubview:self.icon];
    
    
    self.text0.frame = CGRectMake(0, 360 * XHIGHT, WIDTH, 40 * XHIGHT);
    //    self.text0.backgroundColor = [UIColor redColor];
    [self.backImageView addSubview:self.text0];
    self.text0.textColor = [UIColor whiteColor];
    self.text0.font = [UIFont systemFontOfSize:17 * XWIDTH];
    [self.text0 setTextAlignment:NSTextAlignmentCenter];
    
    self.info.frame = CGRectMake(0, 410 * XWIDTH, WIDTH, 40 * XHIGHT);
    //    self.info.backgroundColor = [UIColor redColor];
    [self.backImageView addSubview:self.info];
    self.info.textColor = [UIColor whiteColor];
    self.info.font = [UIFont systemFontOfSize:17 * XWIDTH];
    [self.info setTextAlignment:NSTextAlignmentCenter];
    
    self.title1.frame = CGRectMake(120 * XWIDTH, 460 * XHIGHT, 60 * XWIDTH, 30 * XHIGHT);
    //    self.title1.backgroundColor = [UIColor redColor];
    [self.backImageView addSubview:self.title1];
    self.title1.textColor = [UIColor whiteColor];
    self.title1.font = [UIFont systemFontOfSize:17 * XWIDTH];
    [self.title1 setTextAlignment:NSTextAlignmentCenter];
    
    self.text1.frame = CGRectMake(200 * XWIDTH, 460 * XHIGHT, 60 * XWIDTH, 30 * XHIGHT);
    //    self.text1.backgroundColor = [UIColor redColor];
    [self.backImageView addSubview:self.text1];
    self.text1.textColor = [UIColor whiteColor];
    self.text1.font = [UIFont systemFontOfSize:17 * XWIDTH];
    
    self.title2.frame = CGRectMake(120 * XWIDTH, 510 * XHIGHT, 60 * XWIDTH, 30 * XHIGHT);
    //    self.title2.backgroundColor = [UIColor redColor];
    [self.backImageView addSubview:self.title2];
    self.title2.textColor = [UIColor whiteColor];
    self.title2.font = [UIFont systemFontOfSize:17 * XWIDTH];
    [self.title2 setTextAlignment:NSTextAlignmentCenter];
    
    self.text2.frame = CGRectMake(200 * XWIDTH, 510 * XHIGHT, 60 * XWIDTH, 30 * XHIGHT);
    //    self.text2.backgroundColor = [UIColor redColor];
    [self.backImageView addSubview:self.text2];
    self.text2.textColor = [UIColor whiteColor];
    self.text2.font = [UIFont systemFontOfSize:17 * XWIDTH];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
